22 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
23 #define D(x) cout << #x " is " << x << endl
32 matrix(int a
, int b
, int c
, int d
) : a(a
), b(b
), c(c
), d(d
) {}
33 const matrix
operator * (const matrix
&t
){
34 long long A
= 1LL * a
* t
.a
+ 1LL * b
* t
.c
;
35 long long B
= 1LL * a
* t
.b
+ 1LL * b
* t
.d
;
36 long long C
= 1LL * c
* t
.a
+ 1LL * d
* t
.c
;
37 long long D
= 1LL * c
* t
.b
+ 1LL * d
* t
.d
;
38 if (A
>= mod
) A
%= mod
;
39 if (B
>= mod
) B
%= mod
;
40 if (C
>= mod
) C
%= mod
;
41 if (D
>= mod
) D
%= mod
;
42 return matrix(A
, B
, C
, D
);
47 matrix
pow(const matrix
&p
, int n
){
49 matrix k
= pow(p
, n
/2);
51 if (n
& 1) ans
= ans
* p
;
57 while (cin
>> n
>> m
){
59 if (n
== 0){ cout
<< 0 << endl
; continue; }
60 matrix fib
= pow(matrix(0, 1, 1, 1), n
);
61 cout
<< fib
.b
<< endl
;
66 It can be proved by induction that:
69 | 0 1 | = | fib(n-1) fib(n) |
70 |_0 1_| |_ fib(n) fib(n+1) _|